<template>
  <div class="user-info-menu">
    <el-popover popper-class="user-info-poppor" placement="bottom-end" trigger="hover">
      <el-avatar slot="reference" class="user-icon" :src="circleUrl" size="medium"></el-avatar>
      <div>
        <div v-if="userInfo" class="user-info">
          <i class="el-icon-user-solid"></i>
          <span>{{ userInfo.userName || userInfo.loginName }}</span>
        </div>
        <div class="btn-item" @click="logout">
          <img src="@images/home/icon-logout.svg" alt="" />
          <span class="btn-txt">退出</span>
        </div>
      </div>
    </el-popover>
  </div>
</template>

<script>
import { logout } from '@api/modules/user.js'
import { mapGetters } from 'vuex'
export default {
  name: 'UserInfoIcon',
  components: {},
  props: {},
  data() {
    return {
      circleUrl: require('@images/user-icon.png'),
    }
  },
  computed: {
    ...mapGetters('role', ['userInfo']),
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    logout() {
      this.$confirm('确定退出登录？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info',
      }).then(() => {
        logout()
          .then(res => {
            if (res.status === 200) {
              this.$message.success('退出成功！')
              this.onLogout()
            }
          })
          .catch(() => {
            this.onLogout()
          })
      })
    },
    onLogout() {
      localStorage.removeItem('token')
      localStorage.clear()
      sessionStorage.clear()
      // 清空 store
      this.$store.commit('clearCommonAll')
      this.$router.replace('/login')
    },
  },
}
</script>
<style lang="scss" scoped>
.user-info-menu {
  .user-icon {
    cursor: pointer;
  }
}
</style>

<style lang="scss">
.user-info-poppor {
  .user-info {
    padding: 10px;
    background: #f7f7f7;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    max-width: 200px;
    i {
      margin-right: 8px;
      font-size: 16px;
    }
  }
  .btn-item {
    cursor: pointer;
    height: 45px;
    padding: 10px;
    position: relative;
    font-size: 16px;
    &:hover {
      background: #f7f7f7;
    }
    img {
      vertical-align: middle;
      display: inline-block;
      height: 14px;
    }
    .btn-txt {
      color: #8c8c8c;
      vertical-align: middle;
      margin-left: 10px;
    }
  }
}
</style>
